import axios from 'axios'
import {
	openLoading,
	closeLoading,
	showErrorToast
} from '@/utils'
import ENV from '@/configs/index.js'
import {
	errorHandle,
} from "@/api/dictionary";
import qs from 'qs'

const {
	WebManager,
} = ENV

// 初始化loading
let isShowLoading = true;
let isHandleData = false;

/**
 * 请求封装
 * @param {Boolean} defaultHandle 是否对返回数据进行处理
 * @param {Boolean} showLoading 是否显示加载loading
 * @param {Boolean} showError 是否显示请求错误
 */
const request = ({
	defaultHandle = true,
	showLoading = true,
	showError = true,
	...options
}) => {
	showLoading && openLoading()
	// #ifdef MP-WEIXIN
	// 非线上版本微信小程序环境变量切换
	const {
		miniProgram: {
			envVersion
		}
	} = uni.getAccountInfoSync()
	const ENV_TYPE = uni.getStorageSync('ENV_TYPE')
	if (envVersion !== 'release' && ENV_TYPE) {
		const {
			url
		} = options
		options.url = matchEnv(url, ENV_TYPE)
	}
	// #endif
	return new Promise((resolve, reject) => {
		const access_token = uni.getStorageSync('access_token') || ''
		uni.request({
			url: options.url,
			method: options.method || 'POST',
			header: {
				Authorization: isHandleData ? '' : access_token
			},
			data: isHandleData ? qs.stringify(options.data) : options.data,
			success(res) {
				showLoading && closeLoading();

				if (res.statusCode === 200) {
					resolve(res.data.data || {});
					// return Promise.resolve(res.data.data || {});
				} else {
					if (res.data.message) showErrorToast(res.data.message);
				}
			},
			fail(err) {
				console.log("err", err, options)
				showLoading && closeLoading();
				showError && store.commit('showToast', {
					type: 'error',
					title: '出错了'
				});
			}
		})
	})
}

export const fetchGet = (
	url,
	config = {
		showLoading: true,
		showError: true,
		isHandleData: false,
		isHandleResponseData: true //是否处理响应
	},
) => {
	isShowLoading = config.showLoading; //是否显示loading
	isHandleData = config.isHandleData //是否处理请求data
	return request({
		url: WebManager + url,
		method: 'GET'
	})
}

export const fetchPost = (
	url,
	params = {},
	config = {
		showLoading: true,
		showError: true,
		isHandleData: false,
		isHandleResponseData: true //是否处理响应
	},
) => {
	isShowLoading = config.showLoading; //是否显示loading
	isHandleData = config.isHandleData //是否处理请求data
	return request({
		url: WebManager + url,
		data: params
	})
}

export const fetchPut = (
	url,
	params = {},
	config = {
		showLoading: true,
		showError: true,
		isHandleData: false,
		isHandleResponseData: true
	},
) => {
	isShowLoading = config.showLoading ? config.showLoading : true; //是否显示loading
	isHandleData = config.isHandleData //是否处理请求data
	return request({
		url: WebManager + url,
		data: params,
		method: 'PUT'
	})
}

export const fetchDelete = (
	url,
	params = {},
	config = {
		showLoading: true,
		showError: true,
		isHandleData: false,
		isHandleResponseData: true
	},
) => {
	isShowLoading = config.showLoading ? config.showLoading : true; //是否显示loading
	isHandleData = config.isHandleData //是否处理请求data
	return request({
		url: WebManager + url,
		data: params,
		method: 'DELETE'
	})
}